package com.wxw.BaseLibrary.tool;

import java.text.DecimalFormat;

/**
 * Created by david on 2014-06-11.
 */
public class Math {
    public static boolean isNumberString(String testString)
    {
        if(testString.length()<=0) {
            return false;
        }
        for (Character character : testString.toCharArray()){
            if (!Character.isDigit(character)) {
                return false;
            }
        }


//        String numAllString="0123456789";
//
//        for(int i=0;i<testString.length();i++)
//        {
//            String charInString=testString.substring(i, i+1);
//            if(!numAllString.contains(charInString))
//                return false;
//        }
        return true;
    }

    public static boolean isFloatString(String testString)
    {
        if(!testString.contains(".")) {
            return isNumberString(testString);
        } else {
            String[] floatStringPartArray=testString.split("\\.");
            if(floatStringPartArray.length==2) {
                if(isNumberString(floatStringPartArray[0]) && isNumberString(floatStringPartArray[1])) {
                    return true;
                } else {
                    return false;
                }
            }
            else {
                return false;
            }
        }
    }

    public static double formatDouble(double value, int len) {
        String pattern = "##0.0";
        for (int i=1;i<len;i++) {
            pattern += "0";
        }
        return Double.parseDouble(new DecimalFormat(pattern).format(value));
    }

    public static double formatDouble(double value){
        return Double.parseDouble(new DecimalFormat("##0.00").format(value));
    }

    public static float formatFloat(float value, int len) {
        String pattern = "##0.0";
        for (int i=1;i<len;i++) {
            pattern += "0";
        }
        return Float.parseFloat(new DecimalFormat(pattern).format(value));
    }

    public static float formatFloat(float value){
        return Float.parseFloat(new DecimalFormat("##0.00").format(value));
    }
}
